package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.shisfish.library.Impl.MessyImpl;
import com.shisfish.library.Model.Messy;
import com.shisfish.library.Prere.GetSysMailbox1;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.CacheUtils;
import com.shisfish.library.Utils.EncryptUtils;
import com.shisfish.library.Utils.RandomUtils;
import com.shisfish.library.Utils.SendEmailUtils;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;

@Path("messy")
public class MessyController extends GlobalVariable {

	private MessyImpl messyImpl = new MessyImpl();
	
	@POST
	@Path("insertOne")
	@Produces("application/json; charset=utf-8")
	public String insertOne(@FormParam("code") int code, @FormParam("name") String name, @FormParam("fcode") int fcode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		Messy messy = new Messy(code, name, "", fcode);
		
		int flag = messyImpl.insertOne(messy);
		if (flag == 1) {
			globalMap.put(STATUS_CODE, SUCCESS);
		} else {
			globalMap.put(STATUS_CODE, INSERT_WR);
		}
		
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("insertASysMailbox/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertASysMailbox(@PathParam("idadmin") String idadmin, @FormParam("email") String email, 
			@FormParam("password") String password, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			boolean state = false;
			
			// 先找有木有1的类
			List<Messy> messys = messyImpl.selectByFcode(1);
			
			if (messys.size() > 0) {
				// 删了该数据
				
				int flag = messyImpl.deleteAllByFcode(1);
				
				if (flag != 0) {
					state = true;
				}
				
			}
			else {
				state = true;
			}
			
			if (state) {
				messys.clear();
				
				String ran = RandomUtils.getRandomString();
				String pass = EncryptUtils.convertMD5(password + ran);
				System.out.println(pass);
				Messy me1 = new Messy(1, "系统邮箱", email, 1);
				Messy me2 = new Messy(2, "邮箱密码", pass, 1);
				Messy me3 = new Messy(3, "随机字符串", ran, 1);
				
				messys.add(me1);
				messys.add(me2);
				messys.add(me3);
			}
			
			
			int flag = messyImpl.insertAll(messys);
			
			if (flag == 3) {
				globalMap.put(STATUS_CODE, SUCCESS);
				
				
				// 把系统邮箱存到缓存中
				Cache cache = CacheUtils.getCache(SYS_MAILBOX, request);
				
				if (cache != null) {
					cache.put(new Element("email", email));
					cache.put(new Element("pass", password));
					cache.put(new Element("ran", "ran"));
				}
				
				
			} else {
				globalMap.put(STATUS_CODE, INSERT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("testSysMailbox")
	@Produces("application/json; charset=utf-8")
	public String testSysMailbox(@Context HttpServletRequest request) {
		
		String[] str = GetSysMailbox1.getSysMailbox(request);
		
		String to = "1119078870@qq.com";
		String subject = "testSysMailbox";
		
		String content = RandomUtils.getRandomString(4);
		
		SendEmailUtils.sendEmail(to, subject, content, str[0], str[0], str[1]);
		
		return null;
	}
	
}
